package com.hymjweb.demo.hymj.finance_manage.repayment.controller;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.finance_manage.repayment.bean.CwRepaymentPojo;
import com.hymjweb.demo.hymj.finance_manage.repayment.service.CwRepaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/pages/finance_manage/repayment")
public class CwRepaymentController extends BaseController {

    @Autowired
    private CwRepaymentService cwRepaymentService;

    @RequestMapping("/showCwAddRepayment")
    public String showCwAddRepayment() {
        setRequestAttribute("registUserId", this.getLoginUserId());
        setRequestAttribute("registDate", DateUtil.dateToStr_yyyy_MM_dd(DateUtil.getSysDate()));
        return "/pages/finance_manage/repayment/addCwRepayment";
    }

    @RequestMapping("/showCwRepaymentList")
    public String showCwRepaymentList() {
        return "/pages/finance_manage/repayment/cwRepaymentList";
    }

    @RequestMapping("/showReverseList")
    public String showReverseList() {
        return "/pages/finance_manage/repayment/showReverseList";
    }


    @RequestMapping("/queryRepaymentList")
    @ResponseBody
    public Object queryRepaymentList(@RequestParam(required = false) Map param) {
        return cwRepaymentService.queryRepaymentPageList(param);
    }

    @RequestMapping("/queryReverseList")
    @ResponseBody
    public Object queryReverseList(@RequestParam(required = false) Map param) throws Exception {
        return cwRepaymentService.queryReversePageList(param);
    }

    @RequestMapping("/addRepayment")
    @ResponseBody
    public Object addRepayment(@RequestBody(required = true) JSONObject jsonRepaymentPojo) throws Exception {
        if (jsonRepaymentPojo == null || jsonRepaymentPojo.isEmpty()) {
            throwException("添加发货人还款参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("保存发货人还款成功!");

        // json 对象转 Java 对象
        CwRepaymentPojo cwRepaymentPojo = cwRepaymentService.convertJSONObjToJavaObj(jsonRepaymentPojo);

        // 写入主单信息与主单配置的分单信息
        int effectRows = cwRepaymentService.addRepayment(cwRepaymentPojo);

        if (effectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("添加发货人还款失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/updateAuditStatus")
    @ResponseBody
    public Object clearedBillCharge(@RequestBody JSONObject auditObj) throws Exception {

        if (auditObj == null || auditObj.isEmpty()) {
            throwException("还款审核参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("还款审核成功!");

        int effectRows = cwRepaymentService.updateAuditStatus(auditObj);

        if (effectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("还款审核失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/addReverse")
    @ResponseBody
    public Object addReverse(@RequestBody JSONObject reverseObj) throws Exception {
        if (reverseObj == null || reverseObj.isEmpty()) {
            throw new Exception("分单销账参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("分单销账成功!");

        int effectRows = cwRepaymentService.addReverse(reverseObj);

        if (effectRows <= 0) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("分单销账失败!");
        }

        return baseResultMap;
    }

}
